const {
  MarketingModel
} = require('../../../../utils/marketing-model.js');
var marketingModel = new MarketingModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectType: 0, //0.奖品，1.碎片
    dataItem: [], //数据集
    isHints: false,
    isHintsTitle: "",
    activityInfo: {}, //活动详情
    luckybagShow: false,
    isLoadData: false,
    pageIndex: 1,
    pageSize: 20,
    isScroll: false,
    loadText: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 滚动到底部/右边时触发
   */
  scrollEnd: function () {
    let that = this;
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      isLoadData: true,
      loadText: "加载中..."
    }, function () {
      if (that.data.isScroll) {
        that.setData({
          isLoadData: true,
          loadText: "已经到底了"
        });
      } else {
        that.selcetData();
      }
    })
  },
  /**
   * 奖品列表
   */
  getLightingList: function (type) {
    var that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId'); //商户ID
    param.offset = (that.data.pageIndex - 1) * that.data.pageSize;
    param.limit = that.data.pageSize;
    marketingModel.lightingList(param).then(data => {
      if (data.data.length < that.data.pageSize) {
        that.setData({
          isScroll: true
        })
      }
      if (data && data.data && data.data.length > 0) {
        var newDataItem = that.data.dataItem.concat(data.data);
        that.setData({
          dataItem: newDataItem,
          isHints: false
        })
      } else {
        that.setData({
          loadText: "",
          isLoadData: false,
          isHints: false,
        })
        if (data.totalCount == 0) {
          that.setData({
            dataItem: [],
            isHints: true,
            isHintsTitle: "暂无奖品！"
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 碎片列表
   */
  getfragmentList: function (type) {
    var that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId'); //商户ID
    param.offset = (that.data.pageIndex - 1) * that.data.pageSize;
    param.limit = that.data.pageSize;
    marketingModel.fragmentList(param).then(data => {
      if (data.data.length < that.data.pageSize) {
        that.setData({
          isScroll: true
        })
      }
      if (data && data.data && data.data.length > 0) {
        var newDataItem = that.data.dataItem.concat(data.data);
        that.setData({
          dataItem: newDataItem,
          isHints: false
        })
      } else {
        that.setData({
          loadText: "",
          isLoadData: false,
          isHints: false,
        })
        if (data.totalCount == 0) {
          that.setData({
            dataItem: [],
            isHints: true,
            isHintsTitle: "暂无碎片！"
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 切换导航栏
   */
  selectTab(event) {
    var type = event.currentTarget.dataset.type; //页面类型
    this.setData({
      selectType: type,
      isLoadData: false,
      pageIndex: 1,
      pageSize: 20,
      isScroll: false,
      loadText: ""
    }, function () {
      this.selectTypeList();
    })
  },



  /**
   * 查询奖品碎片
   */
  selectTypeList: function () {
    this.setData({
      dataItem: [],
      isHints: false,
      isHintsTitle: "",
    }, function () {
      this.selcetData()
    })
  },

  /**
   * 查询接口数据
   */
  selcetData: function () {
    if (this.data.selectType == "0") { //奖品
      this.getLightingList();
    } else if (this.data.selectType == "1") {
      this.getfragmentList(); //碎片
    }
  },

  /**
   * 碎片详情页
   */
  goPrizePiecesInfo: function (event) {
    wx.setStorageSync('prizeSelectType', '');
    var type = event.currentTarget.dataset.type; //页面类型
    var activityId = event.currentTarget.dataset.id; //核销记录id || 奖品id
    var activityStatus = event.currentTarget.dataset.status; //状态
    var info = event.currentTarget.dataset.info; //活动详情
    var activityCardType = info.activityType || '';//活动类型  1:老集卡 2:全国集卡 3:达人机 5:瓜分游戏币
    if (activityCardType == 1 || activityCardType == 5 || this.data.selectType == "1") { 
      wx.navigateTo({
        url: `/pages/memberCenter/prize/prizePiecesInfo/index?type=${type}&activityId=${activityId}&activityStatus=${activityStatus}&activityCardType=${activityCardType}`
      })
    } else if (activityCardType == 2) {
      wx.navigateTo({
        url: `/pages/activity/collectCards/prizeInfo/index?activityCardId=${info.activityCardId}`
      })
    } else if (activityCardType == 3) {
      this.setData({
        activityInfo: info,
        luckybagShow: true
      })
    }
  },

  /**
   * 立即领取
   */
  getPrize() {
    let that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId'); //商户ID
    param.activityVerificationId = that.data.activityInfo.activityVerificationId; //核销ID
    param.activityCardType = 3; //活动类型：1：集卡活动一期 2：全国活动 3：达人机活动
    param.activityCardId = that.data.activityInfo.activityCardId; //活动id
    marketingModel.verificationGet(param).then(data => {
      that.setData({
        luckybagShow: false
      }, function () {
        wx.showToast({
          title: `领取成功！`,
          icon: 'success',
          duration: 2000
        })
        this.setData({
          selectType: 0,
          isLoadData: false,
          pageIndex: 1,
          pageSize: 20,
          isScroll: false,
          loadText: ""
        }, function () {
          setTimeout(that.selectTypeList, 1000);
        })
      })
    }).catch(err => {
      that.setData({
        luckybagShow: false
      }, function () {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    })
  },

  /**
   * 关闭领取福袋弹出层
   */
  closeLuckybag() {
    this.setData({
      luckybagShow: false
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this;
    that.setData({
      isLoadData: false,
      pageIndex: 1,
      pageSize: 20,
      isScroll: false,
      loadText: ""
    })
    let prizeSelectType = wx.getStorageSync('prizeSelectType')
    if (parseInt(prizeSelectType) == 0) {
      that.setData({
        selectType: 0,
      }, function () {
        that.selectTypeList();
      })
    } else {
      that.selectTypeList();
    }
  },
})